#include "ToothManager.h"
#include "Tooth1.h"
#include "Tooth2.h"
#include "Tooth3.h"
#include "GameUtil.h"

ToothManager::ToothManager(CCLayer* layer)
{
	_layer = layer;

	// todo
	_teeth = new CCArray;
	_winSize = CCDirector::sharedDirector()->getWinSize();
	initToothPosition();

	// todo, follow data need  read from settings
	_currentMaxTeeth = MAX_TEETH;

	_teethType[0] = TOOTH_TYPE_3;
	_teethType[1] = TOOTH_TYPE_2;
	_teethType[2] = TOOTH_TYPE_1;
	_teethType[3] = TOOTH_TYPE_2;
	_teethType[4] = TOOTH_TYPE_3;

	this->getTeethReady();
}

ToothManager::~ToothManager(void)
{
	// _teeth->release();
}

void ToothManager::removeTooth(Tooth* tooth)
{
	_teeth->removeObject(tooth);
	_layer->removeChild(tooth);
}

void ToothManager::getTeethReady(void)
{
	for (int i = 0; i < _currentMaxTeeth; i++) {
		Tooth* tooth = NULL;
		switch(_teethType[i]) {
			case TOOTH_TYPE_1:
				 tooth = Tooth1::create();
				break;
			case TOOTH_TYPE_2:
				 tooth = Tooth2::create();
				break;
			case TOOTH_TYPE_3:
				 tooth = Tooth3::create();
				break;
			default:
				return;
		}

		tooth->setPosition(_positions[i]);
		_teeth->addObject(tooth);
		_layer->addChild(tooth, 1);

	}

}

void ToothManager::teethAmountUpgrade(void)
{
	assert(_currentMaxTeeth + 1 <= MAX_TEETH);
	_currentMaxTeeth++;
}

void ToothManager::initToothPosition(void)
{
	for (int i = 0; i < MAX_TEETH; i++) {
		_positions[i] = CCPoint(TOOTH_X, GameUtil::getCreatureY(i+1));
	}
}

void ToothManager::startShoot()
{
	CCObject *obj = NULL;  

	CCARRAY_FOREACH(_teeth, obj)
	{
		Tooth* tooth = (Tooth*)obj;
		tooth->startShoot(false);
	}
}

void ToothManager::stopShoot()
{
	CCObject *obj = NULL;  

	CCARRAY_FOREACH(_teeth, obj)
	{
		Tooth* tooth = (Tooth*)obj;
		tooth->stopShoot();
	}
}

void ToothManager::updateTeeth()
{
	CCObject *obj = NULL;  
	CCARRAY_FOREACH(_teeth, obj)
	{
		Tooth* tooth = (Tooth*)obj;
		tooth->onUpdrade();
	}
}